package cn.mymall.web.controller;

import cn.mymall.entity.Member;
import cn.mymall.entity.MemberAddress;
import cn.mymall.entity.mall.CartItem;
import cn.mymall.entity.mall.Product;
import cn.mymall.entity.mall.Sku;
import cn.mymall.service.IMemberAddressService;
import cn.mymall.service.mall.ICartItemService;
import cn.mymall.service.mall.IProductService;
import cn.mymall.service.mall.ISkuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.support.PropertyComparator;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

@Controller
public class SettleController extends BaseController{
    @Autowired
    private ICartItemService cartItemService;
    @Autowired
    private IProductService productService;
    @Autowired
    private ISkuService skuService;
    @Autowired
    private IMemberAddressService memberAddressService;
    @RequestMapping("/home/settle/cart")
    public String product(String cartIds, Integer addressId, ModelMap model) {
        Member member = getLoginMember();
        List<CartItem> list = cartItemService.listByIds(Arrays.asList(cartIds.split(",")))
                .stream().filter(e->e.getMemberId().equals(member.getId())).collect(Collectors.toList());
        Collections.sort(list, new PropertyComparator<>("addTime", false, false));
        MemberAddress memberAddress = null;
        if(addressId!=null){
            memberAddress = memberAddressService.getById(addressId);
            if(!memberAddress.getMemberId().equals(member.getId())){
                memberAddress = null;
            }
        }else {
            memberAddress = memberAddressService.getDefaultAddress(member.getId());
        }
        List<Map> resMapList = new ArrayList();
        BigDecimal totalSettleAmount = BigDecimal.ZERO;
        for (CartItem item : list) {
            Sku sku = skuService.getById(item.getSkuId());
            Product product = productService.getById(sku.getProductId());
            Map map = new HashMap();
            map.put("cartItemId", item.getId());
            map.put("productId", item.getProductId());
            map.put("vendorId", product.getVendorId());
            map.put("categoryId", product.getCategoryId());
            map.put("qty", item.getQty());
            map.put("sellPrice", item.getSellPrice());
            map.put("skuId", item.getSkuId());
            map.put("productName", product.getName());
            map.put("skuName", sku.getName());
            map.put("logo", product.getLogo());
            map.put("booking", sku.hasBooking() && product.hasBooking());
            totalSettleAmount = totalSettleAmount.add(item.getSellPrice().multiply(BigDecimal.valueOf(item.getQty())));
            resMapList.add(map);
        }
        model.put("cartList", resMapList);
        model.put("memberAddress", memberAddress);
        model.put("totalSettleAmount", totalSettleAmount);
        return "front/home/settle_cart.ftl";
    }
    @RequestMapping("/home/settle/product")
    public String product(Integer skuId, Integer qty, Integer addressId, ModelMap model) {
        Member member = getLoginMember();
        MemberAddress memberAddress = null;
        if(addressId!=null){
            memberAddress = memberAddressService.getById(addressId);
            if(!memberAddress.getMemberId().equals(member.getId())){
                memberAddress = null;
            }
        }else {
            memberAddress = memberAddressService.getDefaultAddress(member.getId());
        }
        Sku sku = skuService.getById(skuId);
        Product product = productService.getById(sku.getProductId());
        model.put("product", product);
        model.put("sku", sku);
        model.put("memberAddress", memberAddress);
        model.put("totalSettleAmount", sku.getSellPrice().multiply(BigDecimal.valueOf(qty)));
        return "front/home/settle_product.ftl";
    }
}
